package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加至购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addToCart(ShoppingCartDTO shoppingCartDTO) {
        //查询菜品或套餐是否已在在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.queryCartBydishIdOrSetmealId(shoppingCart);

        //在，直接将对应的数量加一
        if (shoppingCartList != null && !shoppingCartList.isEmpty()){
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //不在，将菜品或套餐数据加入到购物车里面
            //判断当前是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                //是菜品
                DishVO dishVO = dishMapper.queryDishById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setImage(dishVO.getImage());

            }else {
                //是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                SetmealVO setmealVO = setmealMapper.qurrySetmealById(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //将购物车加入数据库
            shoppingCartMapper.addToCart(shoppingCart);
        }


    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> queryCart() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        //查询用户的购物车    查询的SQL这里复用
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.queryCartBydishIdOrSetmealId(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanCart() {
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanCart(userId);
    }

    /**
     * 删除购物车中的商品
     * @param shoppingCartDTO
     */
    @Override
    public void updateShoppingCartItem(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询套餐或菜品的数量
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.queryCartBydishIdOrSetmealId(shoppingCart);
        ShoppingCart cart = shoppingCartList.get(0);
        //菜品数量已经为1了，直接删除
        if (cart.getNumber()==1){
            shoppingCartMapper.deleteShoppingCartItem(cart);
        }else {
            //菜品数量大于1，减少菜品的数量
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.updateNumberById(cart);
        }

    }
}
